// pages/scan/add/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: 0, cateList: [], cate_id: 0,
    code: '', show_code: '',
    title: '', detail: '', thumb: '',
    shequ_name: '', cate_id: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var uinfo = wx.getStorageSync('uinfo');
    if (!uinfo) {
      wx.showToast({
        title: '没有登录', icon: 'none',
      }); return
    }
    this.setData({
      code: options.code,
      show_code: options.show_code,
      uid: uinfo.uid,
    });
    this.cateList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 顶部分类
  cateList: function () {
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'cate/plist',
      data: {  },
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({
          cateList: res.data,
        });
      },
      error: function (err) {
        console.log(err);
      }
    });
  },

  // 选择分类
  toCate: function (e) {
    console.log(e); //return
    var id = e.currentTarget.dataset.id;
    this.setData({ cate_id: id });
  },

  // 选择图片
  chooseImage() {
    var _this = this;
    wx.chooseMedia({
      count: 1, // 最多选择1张
      mediaType: ['image'], // 只允许选择图片
      sourceType: ['album', 'camera'], // 相册和相机
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        console.log(tempFilePath);
        // this.setData({
        //   tempFilePath: tempFilePath // 临时文件路径，用于预览
        // });
        wx.showLoading({
          title: '上传中...',
        });
        wx.uploadFile({
          url: app.globalData.xcxApi + 'scan/img/upload', 
          filePath: tempFilePath,
          name: 'img', 
          formData: {
            uid: this.data.uid,
          },
          success(res) {
            console.log(res);
            wx.hideLoading();
            res = JSON.parse(res.data); // 后端返回的数据
            if (res.code === 0) {
              wx.showToast({
                title: '上传成功',
                icon: 'success'
              });
              _this.setData({
                thumb: res.data.link, 
              });
            } else {
              wx.showToast({
                title: '上传失败',
                icon: 'none'
              });
            }
          },
          fail(err) {
            wx.hideLoading();
            wx.showToast({
              title: '上传失败err', icon: 'none'
            });
          }
        });
      }
    });
  },

  // 保存标题
  toTitle: function (e) {
    console.log(e);
    this.setData({ title: e.detail.value });
  },

  // 保存描述
  toDetail: function (e) {
    console.log(e);
    this.setData({ detail: e.detail.value });
  },

  // 保存社区
  toShequ: function (e) {
    console.log(e);
    this.setData({ shequ_name: e.detail.value });
  },

  // 保存
  toSave: function () {
    var _this = this;
    var cate_id = this.data.cate_id;
    var formData = {
      uid: this.data.uid,
      thumb: this.data.thumb,
      code: this.data.code,
      show_code: this.data.show_code,
      title: this.data.title,
      detail: this.data.detail,
      shequ_name: this.data.shequ_name,
      cate_id: cate_id,
    };
    var urlType = '', prefix = '';
    if (cate_id == 1) {
      prefix = 'yw-user/';
      urlType = 'tmp';
    } else if (cate_id == 2) {
      prefix = 'yw-shop/';
      urlType = 'goods';
    } else if (cate_id == 2) {
      prefix = 'yw-shop/';
      urlType = 'goods';
    } else if (cate_id == 3) {
      prefix = 'yw-shop/';
      urlType = 'fuwu';
    } else {
      wx.showToast({
        title: '类型错误', icon: 'none'
      }); return
    }
    wx.request({
      method: 'POST',
      url: app.globalData.xcxApi + prefix + urlType + '/add',
      data: formData,
      timeout: 10000, //接口超时10S
      success: function (res) {
        res = res.data;
        console.log(res); //return
        if (res.code < 0) {
          wx.showToast({
            title: res.msg, icon: 'none'
          }); return
        }
        wx.navigateTo({
          url: '/pages/scan/' + urlType + '/index?code=' + _this.data.code,
        }); 
      },
      fail: function () {
        console.log('error');
      }
    });
  },
})